<?php
class User_model extends Model {
	/**
	 * Constructor
	 */
	function User_model()
	{
		parent::Model();
	}
	
	// Inisialisasi nama tabel user
	var $table = 'user';
	
	/**
	 * Mendapatkan data semua user
	 */
	function get_all($limit, $offset)
	{
		$this->db->select('user.id_user, user.user_name, usergroup.usergroup, cabang.cabang');
		$this->db->from($this->table);
		$this->db->join('cabang', 'cabang.id_cabang = user.id_cabang');
		$this->db->join('usergroup', 'usergroup.id_usergroup = user.id_usergroup');
		$this->db->limit($limit, $offset);
		$this->db->order_by('id_user', 'asc');
		return $this->db->get()->result();
	}
	
	/**
	 * Mendapatkan data semua user berdasar cabang
	 */
	function get_all_by_cabang($limit, $offset, $id_cabang)
	{
		$this->db->select('user.id_user, user.user_name, usergroup.usergroup, cabang.cabang');
		$this->db->from($this->table);
		$this->db->join('cabang', 'cabang.id_cabang = user.id_cabang');
		$this->db->join('usergroup', 'usergroup.id_usergroup = user.id_usergroup');
		$this->db->limit($limit, $offset);
		$this->db->order_by('id_user', 'asc');
		$this->db->where('user.id_cabang', $id_cabang);
		return $this->db->get()->result();
	}
	
	/**
	 * Mendapatkan data seorang user dengan NIS tertentu
	 */
	function get_user_by_id($id_user)
	{
		return $this->db->get_where($this->table, array('id_user' => $id_user))->row();
	}
	
	/**
	 * Menghitung jumlah baris tabel user
	 */
	function count_all()
	{
		return $this->db->count_all($this->table);
	}
	
	/**
	 * Menghitung jumlah baris tabel user
	 */
	function count_all_by_cabang($id_cabang)
	{
		$this->db->where('id_cabang', $id_cabang);
		return $this->db->count_all($this->table);
	}
	
	/**
	 * Menghapus data user tertentu
	 */
	function delete($id_user)
	{
		$this->db->delete($this->table, array('id_user' => $id_user));
	}
	
	/**
	 * Menambah data user
	 */
	function add($user)
	{
		$this->db->insert($this->table, $user);
	}
	
	/**
	 * Update data user
	 */
	function update($id_user, $user)
	{
		$this->db->where('id_user', $id_user);
		$this->db->update($this->table, $user);
	}
	
	/**
	 * Cek NIS agar tidak ada data user yang sama
	 */
	function valid_id_user($id_user)
	{
		$query = $this->db->get_where($this->table, array('id_user' => $id_user));
		if ($query->num_rows() > 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
}
// END User_model Class

/* End of file user_model.php */
/* Location: ./system/application/models/user_model.php */